//
//  UIViewController+Addition.m
//  todoApp
//
//  Created by wangyaqing on 15/2/26.
//  Copyright (c) 2015年 ll studio. All rights reserved.
//

#import "UIViewController+Addition.h"
#import "NoteDBManger.h"
#import <objc/runtime.h>
#import <MBProgressHUD/MBProgressHUD.h>

#define kDefaultHudDuration 1
static const void *MBProgressHUDKey                   = &MBProgressHUDKey;



@implementation UIViewController (Addition)

- (void)showHudWithTitle:(NSString *)title
{
    [self showHudWithTitle:title andDuration:kDefaultHudDuration];
}

- (void)showHudWithTitle:(NSString *)title andDuration:(NSTimeInterval)timeInterVal
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = title;
    
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, timeInterVal * NSEC_PER_SEC);
    
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // Do something...
        [hud hide:YES];
    });
}

- (NSMutableArray *)existTags
{    
    return [[NoteDBManger shareInstance]queryAllTags];
}

- (void)showIndicatorWithContent:(NSString *)msg
{
    MBProgressHUD *_hud = objc_getAssociatedObject(self, MBProgressHUDKey);
    if (!_hud) {
        _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        _hud.mode = MBProgressHUDAnimationFade;
        objc_setAssociatedObject(self, MBProgressHUDKey, _hud, OBJC_ASSOCIATION_RETAIN);
    }
    
    _hud.labelText = msg;
    [_hud show:YES];
    
}

- (void)hideIndicator
{
    MBProgressHUD *_hud = objc_getAssociatedObject(self, MBProgressHUDKey);

    if (_hud) {
        [_hud hide:YES];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
